import * as Network from '../actions/network.actions';
import {createFeatureSelector, createSelector} from '@ngrx/store';

export interface NetworkState {
  status?: number;
  type?: string;
}

export const initialState: NetworkState = {
  status: 0,
  type: ''
};

export function reducer(state = initialState, action: Network.ActionTypeUnion): NetworkState {
  switch (action.type) {

    case Network.ActionTypes.Connect:
      return {...state, ...action.payload};

    case Network.ActionTypes.Disconnect:
      return {...state, ...action.payload};

    default:
      return state;

  }
}

export const getNetworkState = createFeatureSelector<NetworkState>('networkState');

export const getNetwork = createSelector(
  getNetworkState,
  (state: NetworkState) => state
);
